package com.betterjr.modules.risk.loan.service;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.betterjr.common.utils.BTAssert;
import com.betterjr.modules.agreement.service.ScfElecAgreementService;
import com.betterjr.modules.risk.loan.dao.LoanProjectSynchInterfaces;
import com.betterjr.modules.risk.loan.data.LoanProjectElcAgreementConstantCollections;
import com.betterjr.modules.risk.loan.entity.LoanProjectRelation;

/**
 * 
 * @ClassName: ElcAgreementLoanProjectSynchService 
 * @Description: 贷款项目新建成功或者废止时同步操作电子合同方向服务 
 * @author xuyp
 * @date 2018年6月28日 下午7:57:14 
 *
 */
@Service
public class DataConfirmElcAgreementLoanProjectSynchService implements LoanProjectSynchInterfaces {

    @Autowired
    private ScfElecAgreementService elcAgreementService;

    @Override
    public void InvokeDeleted(LoanProjectRelation anLoan) {

        loggerin.debug(" DataConfirmElcAgreementLoanProjectSynchService  InvokeDeleted " + anLoan);

        try {
            elcAgreementService.saveAndCancelElecLoanAgreement(anLoan.getId(), "11");
        }
        catch (Exception e) {
            BTAssert.notNull(null, "取消确权协议出错" + e.getMessage());
        }

    }

    @Override
    public void InvokeEffective(LoanProjectRelation anLoan) {

        loggerin.debug(" DataConfirmElcAgreementLoanProjectSynchService  InvokeEffective " + anLoan);

        try {
            if (anLoan != null && anLoan.getDataConfirmationAgreement() != null
                    && LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_SUBTYPE_ONLINE
                            .equals(anLoan.getDataConfirmationAgreement().getSubType())
                    && anLoan.getDataConfirmationAgreement().getContractTemplateId() != null
                    && anLoan.getDataConfirmationAgreement().getContractTemplateId() != 0l) {
                elcAgreementService.saveAddElecAgreementByLoanProjectRelation(anLoan,
                        LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_BUSINTYPE_DATACONFIGM,
                        Arrays.asList(anLoan.getFactoryNo(), anLoan.getCoreNo()), anLoan.getCoreName() + "贸易合同确权电子合同");

            }
        }
        catch (Exception e) {
            BTAssert.notNull(null, "创建确权协议出错" + e.getMessage());
        }

    }

}
